package org.biloxi.game.gamerules.playerlost;

import org.biloxi.commands.Command;
import org.biloxi.commands.CommandRepository;
import org.biloxi.commands.Filter;
import org.biloxi.commands.Listener;
import org.biloxi.commands.draw.Draw;
import org.biloxi.model.GameState;
import org.biloxi.model.player.Zone;

/**
 * This GameRule is responsible detecting that a player has lost due to being "milled".
 * Specifically listens to the {@link Draw} command, when a player draws a null cards.
 * This means their Library {@link Zone} was empty and they've been "milled".    
 * 
 * @author Peter.WM.Wong@gmail.com
 *
 */
public class Milled extends PlayerLostRule {
	
	/**
	 * Attaches rule by registering a listener to {@link Draw} command.
	 * @param c
	 */
	public Milled(CommandRepository c) {
		super(c);

		// Attach Listeners
		_c.draw().do_(
			new Listener<Draw.Options,Draw.Result>(){ public void handle(Command<Draw.Options, Draw.Result> cmd, Draw.Options options, Draw.Result result, GameState g) {
				losePlayer(result.pid,g);
			}},
			new Filter<Draw.Result>(){ public boolean allow(Draw.Result result){ 
				return (result.card == null); 
			}}
		);
	}

};
